package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.topscore.uke.R;
import com.topscore.uke.entities.ProvinceBean;
import com.topscore.uke.network.request.SaveMemberAddressRequest;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.dialog.CitySelector;
import com.topscore.utillibrary.widget.CircleImageView;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ModifyAddressActivity extends ToolBarCompatActivity implements View.OnClickListener {

    @Bind(R.id.avatar_iv)
    CircleImageView avatarIv;
    @Bind(R.id.member_name_tv)
    TextView memberNameTv;
    @Bind(R.id.member_phone_tv)
    TextView memberPhoneTv;
    @Bind(R.id.name_edt)
    EditText nameEdt;
    @Bind(R.id.phone_edt)
    EditText phoneEdt;
    @Bind(R.id.arrow)
    TextView arrow;
    @Bind(R.id.city_tx)
    TextView cityTx;
    @Bind(R.id.select_city)
    LinearLayout selectCity;
    @Bind(R.id.address_edt)
    EditText addressEdt;
    @Bind(R.id.save_btn)
    Button saveBtn;

    private SaveMemberAddressRequest mSaveAddressRequest;
    private String mMemberName, mPhone;

    private CitySelector mCitySelector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_address);
        ButterKnife.bind(this);
        setCentralTitle("修改地址");
        mSaveAddressRequest = getIntent().getParcelableExtra("address_info");
        mMemberName = getIntent().getStringExtra("memberName");
        mPhone = getIntent().getStringExtra("phone");
        initViews();
    }

    private void initViews() {
        saveBtn.setClickable(false);
        memberNameTv.setText(mMemberName);
        memberPhoneTv.setText(mPhone);
        cityTx.setText(mSaveAddressRequest.getProvince()
                + "\t" + mSaveAddressRequest.getCity()
                + "\t" + mSaveAddressRequest.getDistrict());
        addressEdt.setText(mSaveAddressRequest.getAddress());
        phoneEdt.setText(mSaveAddressRequest.getReceiverContactInfo());
        nameEdt.setText(mSaveAddressRequest.getReceiverName());
        nameEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSaveAddressRequest.setReceiverName(editable.toString());
                setUpSaveButton();
            }
        });
        phoneEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSaveAddressRequest.setReceiverContactInfo(editable.toString());
                setUpSaveButton();
            }
        });
        addressEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSaveAddressRequest.setAddress(editable.toString());
                setUpSaveButton();
            }
        });
        selectCity.setOnClickListener(this);
        saveBtn.setOnClickListener(this);
    }

    private void setUpSaveButton() {
        if (TextUtils.isEmpty(mSaveAddressRequest.getAddress())
                || TextUtils.isEmpty(mSaveAddressRequest.getReceiverContactInfo())
                || TextUtils.isEmpty(mSaveAddressRequest.getReceiverName())
                || TextUtils.isEmpty(mSaveAddressRequest.getCityId())
                || TextUtils.isEmpty(mSaveAddressRequest.getProvinceId())) {
            saveBtn.setBackgroundColor(Color.parseColor("#dcdcdc"));
            saveBtn.setTextColor(Color.parseColor("#999999"));
            saveBtn.setClickable(false);
        } else {
            saveBtn.setBackgroundResource(R.color.colorPrimary);
            saveBtn.setTextColor(getResources().getColor(R.color.white));
            saveBtn.setClickable(true);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.select_city:
                if (mCitySelector == null) {
                    mCitySelector = new CitySelector(mContext)
                            .setOnCitySelectListener((province, city, district) -> {
                                mSaveAddressRequest.setProvinceId(province.getProvinceCode());
                                mSaveAddressRequest.setProvince(province.getProvinceName());
                                mSaveAddressRequest.setCityId(city.getCityCode());
                                mSaveAddressRequest.setCity(city.getCityName());
                                mSaveAddressRequest.setDistrictId(district.getAreaCode());
                                mSaveAddressRequest.setDistrict(district.getAreaName());
                                cityTx.setText(province.getProvinceName()
                                        + "\t" + city.getCityName() + "\t" + district.getAreaName());
                                setUpSaveButton();
                            });
                }
                mCitySelector.show();
                break;
            case R.id.save_btn:
                Intent intent = new Intent();
                intent.putExtra("address_info", mSaveAddressRequest);
                setResult(RESULT_OK, intent);
                onBackPressed();
                break;
        }
    }
}
